package com.sqfw.project.wpReport.service.impl;

import com.sqfw.common.utils.StringUtils;
import com.sqfw.project.wp.domain.WpVisitInfo;
import com.sqfw.project.wp.mapper.WpVisitInfoMapper;
import com.sqfw.project.wpReport.domain.VisitStatistics;
import com.sqfw.project.wpReport.domain.dto.VisitCountDTO;
import com.sqfw.project.wpReport.service.IVisitStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class VisitStatisticsServiceImpl implements IVisitStatisticsService {

    @Autowired
    private WpVisitInfoMapper wpVisitInfoMapper;


    @Override
    public List<VisitStatistics> getVisitStatisticsList(WpVisitInfo wpVisitInfo) {

        //内容分类查询条件
        List<List<String>> classAllFind = wpVisitInfo.getClassAllFind();

        //内容分类查询
        if (StringUtils.isEmpty(classAllFind)) {
            wpVisitInfo.setClassAll(null);
        } else {
            List<String> classAllCon = new ArrayList<>();
            for (List<String> strings : classAllFind) {
                classAllCon.add(strings.get(strings.size() - 1));
            }
            wpVisitInfo.setClassAll(classAllCon);
        }

        Integer visitCount =  wpVisitInfoMapper.selectCountDistentStatisVisitWpid(wpVisitInfo);

        VisitStatistics visitStatistics = new VisitStatistics();

        //包含无需回访的数量
        visitStatistics.setVisitCount(visitCount);

        //无需回访
        wpVisitInfo.setVisitResult("无需回访");
        Integer visitNoCount =  wpVisitInfoMapper.getWpVisitInfoList(wpVisitInfo);
        visitStatistics.setVisitNoCount(visitNoCount);




        //满意
        wpVisitInfo.setIssatisfied("满意");
        Integer satisCount = wpVisitInfoMapper.selectCountDistentStatisVisitWpid(wpVisitInfo);
        visitStatistics.setSatisCount(satisCount);
        //不满意
        wpVisitInfo.setIssatisfied("不满意");
        Integer unSatisCount = wpVisitInfoMapper.selectCountDistentStatisVisitWpid(wpVisitInfo);
        visitStatistics.setUnsatisCount(unSatisCount);
        //一般
        wpVisitInfo.setIssatisfied("一般");
        Integer kindCount = wpVisitInfoMapper.selectCountDistentStatisVisitWpid(wpVisitInfo);
        visitStatistics.setKindCount(kindCount);

        visitStatistics.setVisitSucessCount(satisCount+unSatisCount+kindCount);

        String satisPoint = getSatisRate(visitStatistics.getVisitSucessCount(), satisCount, kindCount);
        visitStatistics.setSatisPoint(satisPoint+"%");


        //不满意中的未答复
        wpVisitInfo.setIssatisfied(null);
        wpVisitInfo.setIsReply("0");
        Integer noReplyCount =  wpVisitInfoMapper.selectCountDistentStatisVisitWpid(wpVisitInfo);
        visitStatistics.setNoReplyCount(noReplyCount);

        //政策所限
        wpVisitInfo.setIsReply(null);
        List<VisitCountDTO> dtoList =  wpVisitInfoMapper.countWpVisitInfoGroupByIssatisfiedReason(wpVisitInfo);
        for (VisitCountDTO visitCountDTO : dtoList) {
            if ("政策所限".equals(visitCountDTO.getReasonType())){
                visitStatistics.setPolicyCount(visitCountDTO.getCountNumber());
            }
            if ("诉求过高".equals(visitCountDTO.getReasonType())){
                visitStatistics.setAppealCount(visitCountDTO.getCountNumber());
            }
            if ("工作人员态度".equals(visitCountDTO.getReasonType())){
                visitStatistics.setAttitudeCount(visitCountDTO.getCountNumber());
            }
            if ("办理时间过长".equals(visitCountDTO.getReasonType())){
                visitStatistics.setLongTimeCount(visitCountDTO.getCountNumber());
            }
            if ("其他".equals(visitCountDTO.getReasonType())){
                visitStatistics.setOtherCount(visitCountDTO.getCountNumber());
            }
        }
        if (visitStatistics.getPolicyCount()==null){
            visitStatistics.setPolicyCount(0);
        }
        if (visitStatistics.getAppealCount()==null){
            visitStatistics.setAppealCount(0);
        }
        if (visitStatistics.getAttitudeCount()==null){
            visitStatistics.setAttitudeCount(0);
        }
        if (visitStatistics.getLongTimeCount()==null){
            visitStatistics.setLongTimeCount(0);
        }
        if (visitStatistics.getOtherCount()==null){
            visitStatistics.setOtherCount(0);
        }

        //不满意中的处理结果
        int processCount = dtoList.stream().map(VisitCountDTO::getCountNumber).reduce(Integer::sum)
                .orElse(0);
        visitStatistics.setProcessCount(processCount);

        List<VisitStatistics> visitStatisticsList = new ArrayList<>();

        visitStatisticsList.add(visitStatistics);

        return visitStatisticsList;
    }


    /**
     * 满意率
     *
     * @return
     */
    public String getSatisRate(int visitSize, int satisSize, int kindSize) {

        if (visitSize==0){
            return "100.00";
        }
        //(满意度*1+一般数*0.6+不满意*0)/总回访数*30
        double v = ((double) satisSize*100 + (double) kindSize * 60) / (double) visitSize;
        //返回结果保留两位小数
        return String.format("%.2f", v );


    }
}
